package com.manolo.client.form;
import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.Font;
import java.awt.Rectangle;
import javax.swing.JLabel;

import com.manolo.client.entity.Player;
import com.manolo.client.entity.Round;
import com.manolo.event.HandCardEvent;
import com.manolo.event.dispatcher.CardDispatcher;
import com.manolo.event.listener.CardListener;

/**
 * An example which shows off a functional simple text editor.  Includes a variety of events.
 */
public class TrucoWindow extends JFrame implements CardListener {

	private javax.swing.JPanel jContentPane = null;
	private JButton jbtnBottomPlayerFirstCard = null;
	private JButton jbtnBottomPlayerSecondCard = null;
	private JButton jbtnBottomPlayerThirdCard = null;
	private JButton jbtnLeftPlayerFirstCard = null;
	private JButton jbtnLeftPlayerSecondCard = null;
	private JButton jbtnLeftPlayerThirdCard = null;
	private JButton jbtnTopPlayerFirstCard = null;
	private JButton jbtnTopPlayerSecondCard = null;
	private JButton jbtnTopPlayerThirdCard = null;
	private JButton jbtnRightPlayerFirstCard = null;
	private JButton jbtnRightPlayerSecondCard = null;
	private JButton jbtnRightPlayerThirdCard = null;
	private JLabel jlblMainCard = null;
	private JLabel jlblTopPlayer = null;
	private JLabel jlblBottomPlayer = null;
	private JLabel jlblLeftPlayer = null;
	private JLabel jlblRightPlayer = null;
	private JLabel jlblTheirScore = null;
	
	/* Player position enumerator */
	private final int BOTTOM_PLAYER = 1;
	private final int TOP_PLAYER = 2;
	private final int RIGHT_PLAYER = 3;
	private final int LEFT_PLAYER = 4;
	
	/*throw distance for each player*/
	private final int BOTTOM_PLAYER_THROW_DISTANCE = 20;
	private final int TOP_PLAYER_THROW_DISTANCE = 20;
	private final int RIGHT_PLAYER_THROW_DISTANCE = 20;
	private final int LEFT_PLAYER_THROW_DISTANCE = 20;
	
	private final int FIRST_CARD = 1;
	private final int SECOND_CARD = 2;
	private final int THIRD_CARD = 3;
	
	/*information about the players*/
	private Player bottomPlayer;
	private Player leftPlayer;
	private Player topPlayer;
	private Player rightPlayer;
	
	/*information about the current round*/
	private Round currentRound;
	
	/* used to know if its current player turn*/
	private boolean myTurn;
	private JLabel jlblOurScore = null;

	/**
	 * This method initializes 
	 * 
	 */
	public TrucoWindow() {
		super();
		initialize();
		
		CardDispatcher.getInstance().addLocalListener(this);
	}
	
	public TrucoWindow(String bottomPlayer, String leftPlayer, String topPlayer, String rightPlayer) {
		this();
		
		jlblBottomPlayer.setText(bottomPlayer);
		jlblLeftPlayer.setText(leftPlayer);
		jlblTopPlayer.setText(topPlayer);
		jlblRightPlayer.setText(rightPlayer);
		
		jlblOurScore.setText("Our Score: 0");
		jlblTheirScore.setText("Their Score: 0");
		
		setPlayerTurn(BOTTOM_PLAYER);
	}

	/**
	 * This method initializes jbtnBottomPlayerFirstCard	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJbtnBottomPlayerFirstCard() {
		if (jbtnBottomPlayerFirstCard == null) {
			jbtnBottomPlayerFirstCard = new JButton();
			jbtnBottomPlayerFirstCard.setBounds(new Rectangle(140, 340, 90, 40));
			jbtnBottomPlayerFirstCard.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					throwCard(BOTTOM_PLAYER, FIRST_CARD);
				} 
			});
		}
		return jbtnBottomPlayerFirstCard;
	}

	/**
	 * This method initializes jbtnBottomPlayerSecondCard	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJbtnBottomPlayerSecondCard() {
		if (jbtnBottomPlayerSecondCard == null) {
			jbtnBottomPlayerSecondCard = new JButton();
			jbtnBottomPlayerSecondCard.setBounds(new Rectangle(260, 340, 90, 40));
		}
		return jbtnBottomPlayerSecondCard;
	}

	/**
	 * This method initializes jbtnBottomPlayerThirdCard	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJbtnBottomPlayerThirdCard() {
		if (jbtnBottomPlayerThirdCard == null) {
			jbtnBottomPlayerThirdCard = new JButton();
			jbtnBottomPlayerThirdCard.setBounds(new Rectangle(380, 340, 90, 40));
		}
		return jbtnBottomPlayerThirdCard;
	}

	/**
	 * This method initializes jbtnLeftPlayerFirstCard	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJbtnLeftPlayerFirstCard() {
		if (jbtnLeftPlayerFirstCard == null) {
			jbtnLeftPlayerFirstCard = new JButton();
			jbtnLeftPlayerFirstCard.setBounds(new Rectangle(30, 240, 90, 40));
		}
		return jbtnLeftPlayerFirstCard;
	}

	/**
	 * This method initializes jbtnLeftPlayerSecondCard	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJbtnLeftPlayerSecondCard() {
		if (jbtnLeftPlayerSecondCard == null) {
			jbtnLeftPlayerSecondCard = new JButton();
			jbtnLeftPlayerSecondCard.setBounds(new Rectangle(30, 180, 90, 40));
		}
		return jbtnLeftPlayerSecondCard;
	}

	/**
	 * This method initializes jbtnLeftPlayerThirdCard	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJbtnLeftPlayerThirdCard() {
		if (jbtnLeftPlayerThirdCard == null) {
			jbtnLeftPlayerThirdCard = new JButton();
			jbtnLeftPlayerThirdCard.setBounds(new Rectangle(30, 120, 90, 40));
		}
		return jbtnLeftPlayerThirdCard;
	}

	/**
	 * This method initializes jbtnTopPlayerFirstCard	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJbtnTopPlayerFirstCard() {
		if (jbtnTopPlayerFirstCard == null) {
			jbtnTopPlayerFirstCard = new JButton();
			jbtnTopPlayerFirstCard.setBounds(new Rectangle(140, 30, 90, 40));
		}
		return jbtnTopPlayerFirstCard;
	}

	/**
	 * This method initializes jbtnTopPlayerSecondCard	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJbtnTopPlayerSecondCard() {
		if (jbtnTopPlayerSecondCard == null) {
			jbtnTopPlayerSecondCard = new JButton();
			jbtnTopPlayerSecondCard.setBounds(new Rectangle(260, 30, 90, 40));
		}
		return jbtnTopPlayerSecondCard;
	}

	/**
	 * This method initializes jbtnTopPlayerThirdCard	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJbtnTopPlayerThirdCard() {
		if (jbtnTopPlayerThirdCard == null) {
			jbtnTopPlayerThirdCard = new JButton();
			jbtnTopPlayerThirdCard.setBounds(new Rectangle(380, 30, 90, 40));
		}
		return jbtnTopPlayerThirdCard;
	}

	/**
	 * This method initializes jbtnRightPlayerFirstCard	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJbtnRightPlayerFirstCard() {
		if (jbtnRightPlayerFirstCard == null) {
			jbtnRightPlayerFirstCard = new JButton();
			jbtnRightPlayerFirstCard.setBounds(new Rectangle(520, 120, 90, 40));
		}
		return jbtnRightPlayerFirstCard;
	}

	/**
	 * This method initializes jbtnRightPlayerSecondCard	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJbtnRightPlayerSecondCard() {
		if (jbtnRightPlayerSecondCard == null) {
			jbtnRightPlayerSecondCard = new JButton();
			jbtnRightPlayerSecondCard.setBounds(new Rectangle(520, 180, 90, 40));
		}
		return jbtnRightPlayerSecondCard;
	}

	/**
	 * This method initializes jbtnRightPlayerThirdCard	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJbtnRightPlayerThirdCard() {
		if (jbtnRightPlayerThirdCard == null) {
			jbtnRightPlayerThirdCard = new JButton();
			jbtnRightPlayerThirdCard.setBounds(new Rectangle(520, 240, 90, 40));
		}
		return jbtnRightPlayerThirdCard;
	}

	public static void main(String[] args) {
		TrucoWindow ste = new TrucoWindow();
		ste.show();
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJContentPane() {
		if (jContentPane == null) {
			jlblOurScore = new JLabel();
			jlblOurScore.setBounds(new Rectangle(10, 38, 114, 15));
			jlblOurScore.setText("JLabel");
			jlblTheirScore = new JLabel();
			jlblTheirScore.setBounds(new Rectangle(9, 15, 116, 16));
			jlblTheirScore.setText("JLabel");
			jlblRightPlayer = new JLabel();
			jlblRightPlayer.setBounds(new Rectangle(520, 88, 89, 16));
			jlblRightPlayer.setFont(new Font("Dialog", Font.PLAIN, 12));
			jlblRightPlayer.setText("JLabel");
			jlblLeftPlayer = new JLabel();
			jlblLeftPlayer.setBounds(new Rectangle(33, 88, 91, 16));
			jlblLeftPlayer.setFont(new Font("Dialog", Font.PLAIN, 12));
			jlblLeftPlayer.setText("JLabel");
			jlblBottomPlayer = new JLabel();
			jlblBottomPlayer.setBounds(new Rectangle(263, 386, 111, 16));
			jlblBottomPlayer.setFont(new Font("Dialog", Font.PLAIN, 12));
			jlblBottomPlayer.setText("JLabel");
			jlblTopPlayer = new JLabel();
			jlblTopPlayer.setBounds(new Rectangle(251, 8, 113, 16));
			jlblTopPlayer.setFont(new Font("Dialog", Font.PLAIN, 12));
			jlblTopPlayer.setText("JLabel");
			jlblMainCard = new JLabel();
			jlblMainCard.setBounds(new Rectangle(267, 183, 80, 35));
			jlblMainCard.setText("JLabel");
			jContentPane = new javax.swing.JPanel();
			jContentPane.setLayout(null);
			jContentPane.setBorder(javax.swing.BorderFactory.createEmptyBorder(
					5, 5, 5, 5));
			jContentPane.add(getJbtnBottomPlayerFirstCard(), null);
			jContentPane.add(getJbtnBottomPlayerSecondCard(), null);
			jContentPane.add(getJbtnBottomPlayerThirdCard(), null);
			jContentPane.add(getJbtnLeftPlayerFirstCard(), null);
			jContentPane.add(getJbtnLeftPlayerSecondCard(), null);
			jContentPane.add(getJbtnLeftPlayerThirdCard(), null);
			jContentPane.add(getJbtnTopPlayerFirstCard(), null);
			jContentPane.add(getJbtnTopPlayerSecondCard(), null);
			jContentPane.add(getJbtnTopPlayerThirdCard(), null);
			jContentPane.add(getJbtnRightPlayerFirstCard(), null);
			jContentPane.add(getJbtnRightPlayerSecondCard(), null);
			jContentPane.add(getJbtnRightPlayerThirdCard(), null);
			jContentPane.add(jlblMainCard, null);
			jContentPane.add(jlblTopPlayer, null);
			jContentPane.add(jlblBottomPlayer, null);
			jContentPane.add(jlblLeftPlayer, null);
			jContentPane.add(jlblRightPlayer, null);
			jContentPane.add(jlblTheirScore, null);
			jContentPane.add(jlblOurScore, null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setContentPane(getJContentPane());
		this.setSize(660, 449);
		this.setTitle("Truco");
		this.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);


	}
	private void throwCard(int playerSide, int cardIndex)
	{
		switch (playerSide) {
		case BOTTOM_PLAYER:
			if (cardIndex == FIRST_CARD)
				jbtnBottomPlayerFirstCard.setLocation(jbtnBottomPlayerFirstCard.getX(), jbtnBottomPlayerFirstCard.getY() - BOTTOM_PLAYER_THROW_DISTANCE);
			else if (cardIndex == SECOND_CARD)
				jbtnBottomPlayerSecondCard.setLocation(jbtnBottomPlayerSecondCard.getX(), jbtnBottomPlayerSecondCard.getY() - BOTTOM_PLAYER_THROW_DISTANCE);
			else if (cardIndex == THIRD_CARD)
				jbtnBottomPlayerThirdCard.setLocation(jbtnBottomPlayerThirdCard.getX(), jbtnBottomPlayerThirdCard.getY() - BOTTOM_PLAYER_THROW_DISTANCE);
			break;
		case TOP_PLAYER:
			if (cardIndex == FIRST_CARD)
				jbtnTopPlayerFirstCard.setLocation(jbtnTopPlayerFirstCard.getX(), jbtnTopPlayerFirstCard.getY() + TOP_PLAYER_THROW_DISTANCE);
			else if (cardIndex == SECOND_CARD)
				jbtnTopPlayerSecondCard.setLocation(jbtnTopPlayerSecondCard.getX(), jbtnTopPlayerSecondCard.getY() + TOP_PLAYER_THROW_DISTANCE);
			else if (cardIndex == THIRD_CARD)
				jbtnTopPlayerThirdCard.setLocation(jbtnTopPlayerThirdCard.getX(), jbtnTopPlayerThirdCard.getY() + TOP_PLAYER_THROW_DISTANCE);			
			break;
		case LEFT_PLAYER:
			if (cardIndex == FIRST_CARD)
				jbtnLeftPlayerFirstCard.setLocation(jbtnLeftPlayerFirstCard.getX() + LEFT_PLAYER_THROW_DISTANCE, jbtnLeftPlayerFirstCard.getY());
			else if (cardIndex == SECOND_CARD)
				jbtnLeftPlayerSecondCard.setLocation(jbtnLeftPlayerSecondCard.getX() + LEFT_PLAYER_THROW_DISTANCE, jbtnLeftPlayerSecondCard.getY());
			else if (cardIndex == THIRD_CARD)
				jbtnLeftPlayerThirdCard.setLocation(jbtnLeftPlayerThirdCard.getX() + LEFT_PLAYER_THROW_DISTANCE, jbtnLeftPlayerThirdCard.getY());
			break;
		case RIGHT_PLAYER:
			if (cardIndex == FIRST_CARD)
				jbtnRightPlayerFirstCard.setLocation(jbtnRightPlayerFirstCard.getX() - RIGHT_PLAYER_THROW_DISTANCE, jbtnRightPlayerFirstCard.getY());
			else if (cardIndex == SECOND_CARD)
				jbtnRightPlayerSecondCard.setLocation(jbtnRightPlayerSecondCard.getX() - RIGHT_PLAYER_THROW_DISTANCE, jbtnRightPlayerSecondCard.getY());
			else if (cardIndex == THIRD_CARD)
				jbtnRightPlayerThirdCard.setLocation(jbtnRightPlayerThirdCard.getX() - RIGHT_PLAYER_THROW_DISTANCE, jbtnRightPlayerThirdCard.getY());
			break;
		default:
			
			break;
		}
	}

	@Override
	public void handCards(HandCardEvent event) {
		jbtnBottomPlayerFirstCard.setText(event.getCards().get(0).getNumber().toString());
		jbtnBottomPlayerSecondCard.setText(event.getCards().get(1).getNumber().toString());
		jbtnBottomPlayerThirdCard.setText(event.getCards().get(2).getNumber().toString());
		
		
	}
	
	private void setPlayerTurn(int position)
	{
		if(position == BOTTOM_PLAYER){
			Font f = jlblBottomPlayer.getFont();
			jlblBottomPlayer.setFont(f.deriveFont(f.getStyle() & Font.BOLD));
		}
	}
	
} //  @jve:visual-info  decl-index=0 visual-constraint="20,27"
